package com.bcmusic.order.controller;

import com.bcmusic.music.feign.MusicFeign;
import com.bcmusic.order.config.TokenDecode;
import com.bcmusic.order.domain.cart.dto.AddToCartDTO;
import com.bcmusic.order.pojo.OrderCart;
import com.bcmusic.order.service.OrderCartService;
import com.bcmusic.user.pojo.User;
import entity.IdWorker;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/orderCart")
@Api(value = "/orderCart", tags = "购物车模块")
public class OrderCartController {

    @Autowired
    private OrderCartService orderCartService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MusicFeign musicFeign;

    @Autowired
    private TokenDecode tokenDecode;


    /**
     * 添加订单，校验合法性,,,问就是不相信前端
     *
     * @param addToCartDTO
     * @return 返回添加购物车结果
     */
    @ApiOperation(value = "许可类型的添加购物车,注意！！转让类型不能走购物车，要直接购买", notes = "许可类型商品在商品页面调用这个接口将物品添加到购物车中,注意！！转让类型不能走购物车，要直接购买")
    @PostMapping("addToCart")
    public Result addToCart(@RequestBody @Valid AddToCartDTO addToCartDTO) {

        String userNum = tokenDecode.getUserInfo().get("id");
        int i = orderCartService.addToCart(addToCartDTO, userNum);
        switch (i){
            case 0:
                return new Result(true,StatusCode.ERROR,"该发布信息已撤销，请重试");
            case 1:
                return new Result(true,StatusCode.OK,"添加成功");
            case 2:
                return new Result(true,StatusCode.ERROR,"地域不符合规范");
            case 3:
                return new Result(true,StatusCode.ERROR,"日期不正确");
            case 4:
                return new Result(true,StatusCode.ERROR,"授权类型不正确");
            default:
                return new Result(true,StatusCode.ERROR,"添加失败");
        }
    }

    /**
     * 删除购物车中一件商品
     * 未测试
     */
    @ApiOperation(value = "在购物车删除一件商品", notes = "请求路径传入musicNum,删除商品")
    @GetMapping("deleteOneCart/{permissionId}")
    public Result deleteOneCart(@ApiParam(name = "permissionId",value = "对应发布信息的id")@PathVariable Integer permissionId) {
        String userNum = tokenDecode.getUserInfo().get("id");
        if (orderCartService.deleteOneCart(permissionId, userNum)) {
            return new Result<>(true, StatusCode.OK, "删除购物车成功");
        }
        return new Result<>(true, StatusCode.ERROR, "删除购物车成功");
    }

    /**
     * 清空全部购物车
     * 未测试
     * @return
     */
    @ApiOperation(value = "清空购物车", notes = "直接请求路径即可")
    @GetMapping("deleteAllCart")
    public Result deleteAllCart() {
        String userNum = tokenDecode.getUserInfo().get("id");
        Boolean bool = orderCartService.deleteAllCart(userNum);
        if (bool) {
            return new Result<>(true, StatusCode.OK, "清空购物车成功");
        }
        return new Result<>(true, StatusCode.ERROR, "清空购物车失败，请重试");
    }


    /**
     * 获取购物车列表,每次查询列表都要判断一下商品是否已经下架
     *未测试
     * @return
     */
    @GetMapping("list")
    @ApiOperation(value = "获取用户购物车列表", notes = "直接请求路径即可")
    public Result<Map<String,Object>> findList() {
        String userNum = tokenDecode.getUserInfo().get("id");
        Map<String, Object> list = orderCartService.findList(userNum);
        return new Result<>(true, StatusCode.OK, "查询购物车成功", list);
    }

    /**
     *支付成功时查询应该添加的授权信息,后台调用
     *未测试
     * @return
     */
    @ApiIgnore
    @GetMapping("getOrderCartListByOrderId")
    public Result<List<OrderCart>> getOrderCartListByOrderId(@RequestParam Integer orderId) {
        List<OrderCart> orderCartListByOrderId = orderCartService.getOrderCartListByOrderId(orderId);
        return new Result<>(true,StatusCode.OK,"查询成功",orderCartListByOrderId);
    }

}
